package com.example.warehouse_manager.service.impl;

import com.example.warehouse_manager.dto.AssignRoleDto;
import com.example.warehouse_manager.entity.Result;
import com.example.warehouse_manager.entity.User;
import com.example.warehouse_manager.entity.UserRole;
import com.example.warehouse_manager.mapper.RoleMapper;
import com.example.warehouse_manager.mapper.UserMapper;
import com.example.warehouse_manager.mapper.UserRoleMapper;
import com.example.warehouse_manager.page.Page;
import com.example.warehouse_manager.service.UserService;
import com.example.warehouse_manager.utils.DigestUtil;
import com.example.warehouse_manager.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleMapper roleMapper;


    //根据账号查询用户业务的方法
    @Override
    public User queryUserByCode(String userCode) {
        return userMapper.findUserByCode(userCode);
    }

    @Override
    public Page queryUserByPage(Page page, User user) {

        //查询用户行数
        Integer userRowCount = userMapper.findUserRowCount(user);
        //分页查询用户
        List<User> userByPage = userMapper.findUserByPage(user, page);

        //组装分页信息
        page.setTotalNum(userRowCount);
        page.setResultList(userByPage);

        return page;
    }


    //添加用户
    @Override
    public Result saveUser(User user) {

        //判断账号是否存在
        User userByCode = userMapper.findUserByCode(user.getUserCode());
        if (userByCode!=null){//账号已存在
            return Result.err(Result.CODE_ERR_BUSINESS,"账号已存在");
        }

        //对密码进行加密处理
        String s = DigestUtil.hmacSign(user.getUserPwd());
        user.setUserPwd(s);

        //添加
        int i = userMapper.insertUser(user);
        if(i>0){//添加成功
            return Result.ok("用户添加成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"用户添加失败");
    }

    //更改用户禁用、启用状态
    @Override
    public Result updateState(User user) {
        //跟新状态
        int i = userMapper.updateState(user);
        if (i>0){//更新成功
            return Result.ok("用户更新成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"用户更新失败!");
    }

    //给用户分配角色
/*    @Transactional
    @Override
    public void assignRole(AssignRoleDto assignRoleDto) {
        userRoleMapper.removeUserRoleById(assignRoleDto.getUserId());

        List<String> roleCheckList = assignRoleDto.getRoleCheckList();
        for (String roleName : roleCheckList) {
            Integer roleId = roleMapper.findRoleIdByName(roleName);
            UserRole userRole = new UserRole();
            userRole.setRoleId(roleId);
            userRole.setUserId(assignRoleDto.getUserId());
            userRoleMapper.insertUserRole(userRole);
        }
    }*/

    //给用户分配角色的业务方法
    @Transactional//事务处理
    @Override
    public void assignRole(AssignRoleDto assignRoleDto) {

        //拿到用户id
        Integer userId = assignRoleDto.getUserId();
        //拿到给用户分配的所有角色名
        List<String> roleNameList = assignRoleDto.getRoleCheckList();

        //根据用户id删除给用户已分配的所有角色
        userRoleMapper.removeUserRoleById(assignRoleDto.getUserId());

        //循环添加用户角色关系
        for (String roleName : roleNameList) {
            //根据当前角色名查询当前角色的id
            int roleId = roleMapper.getRoleIdByName(roleName);
            //添加用户角色关系
            roleMapper.insertUserRole(userId, roleId);
        }
    }


    //删除用户
    @Override
    public Result removeUserById(List<Integer> userId) {
        int i = userMapper.setIsDeleteByUids(userId);
        if (i > 0){
            return Result.ok("删除成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"删除失败！");
    }

    //修改用户
    @Override
    public Result setUserById(User user) {
        int i = userMapper.setUserNameById(user);
        if (i>0){
            return Result.ok("修改成功!");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"修改失败");
    }

    //根据用户id重置密码
    @Override
    public Result setPwdById(Integer userid) {
        //给点初始密码123456并且加密
        String password = DigestUtil.hmacSign("123456");
        int i = userMapper.setPwdById(userid, password);
        if (i > 0){
            return Result.ok("重置密码成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"重置密码失败");
    }


}
